﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class CustomerUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtNgayThanhLap.Text = DateTime.Now.ToString();
                BindGridDrop();
            }
            if (chkCheck1.Checked == true)
            {
                txtTenDayDu.Enabled = true;
                txtDiaChiHopDong.Enabled = true;
                drTinhHopDong.Enabled = true;
                txtMaSoThue.Enabled = true;
                drNguoiKyHopDong.Enabled = true;
            }
            else
            {
                txtTenDayDu.Enabled = false;
                txtDiaChiHopDong.Enabled = false;
                drTinhHopDong.Enabled = false;
                txtMaSoThue.Enabled = false;
                drNguoiKyHopDong.Enabled = false;
            }
            if (chkCheck2.Checked == true)
            {
                txtDiaChiGiaoNhan.Enabled = true;
                drNguoiNhan.Enabled = true;
            }
            else
            {
                txtDiaChiGiaoNhan.Enabled = false;
                drNguoiNhan.Enabled = false;
            }
            if (chkCheck3.Checked == true)
            {
                txtDiaChiThanhToan.Enabled = true;
                drNguoiThanhToan.Enabled = true;
            }
            else
            {
                txtDiaChiThanhToan.Enabled = false;
                drNguoiThanhToan.Enabled = false;
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertCustomer();
            }
            else
            {
                UpdateCustomer();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindGridDrop();
        }
        protected void InsertCustomer()
        {
            try
            {
                var obj = new Customer_ET
                {
                    Customer_Alias = txtMaKhachHang.Text,
                    Customer_Type_ID = int.Parse(drLoaiKhachHang.SelectedValue),
                    Customer_Shortcut = txtTenVietTat.Text,
                    Customer_Name = txtTenKhachHang.Text,
                    Customer_Name_EN = txtTenTiengAnh.Text,
                    Customer_Name_Exchange = txtTenGiaoDich.Text,
                    Enterprise_ID = int.Parse(drLoaiHinhDoanhNghiep.SelectedValue),
                    Customer_Debt_Limit = float.Parse(txtHanMucNo.Text),
                    Customer_Address = txtDiaChiGiaoDich.Text,
                    Customer_Payment_Deadline = int.Parse(txtHanThanhToan.Text),
                    District_ID = int.Parse(drQuanHuyen.SelectedValue),
                    Customer_Establish = DateTime.Parse(txtNgayThanhLap.Text),
                    Customer_Phone1 = txtDienThoai1.Text,
                    Customer_Phone2 = txtDienThoai2.Text,
                    Customer_Fax = txtFax.Text,
                    Customer_Website = txtWebsite.Text,
                    Sector_ID = int.Parse(drNganhNghe.SelectedValue),
                    Customer_Account_Number = txtSoTaiKhoan.Text,
                    Customer_Contact = int.Parse(drNguoiLienHe.SelectedValue),
                    Bank_ID = int.Parse(drNganHang.SelectedValue),
                    Customer_Contract_FullName = txtTenDayDu.Text,
                    Customer_Contract_Address = txtDiaChiHopDong.Text,
                    Customer_Contract_District = int.Parse(drQuanHuyenHopDong.SelectedValue),
                    Customer_Contract_Tax_Code = txtMaSoThue.Text,
                    Customer_Contract_Contact = int.Parse(drNguoiKyHopDong.SelectedValue),
                    Customer_Delivery_Address = txtDiaChiGiaoNhan.Text,
                    Customer_Delivery_Contact = int.Parse(drNguoiNhan.SelectedValue),
                    Customer_Payment_Address = txtDiaChiThanhToan.Text,
                    Customer_Payment_Contact = int.Parse(drNguoiThanhToan.SelectedValue),
                    Customer_Note = txtGhiChu.Text,
                    Customer_Date_Add = DateTime.Now,
                    Customer_Date_Modify = DateTime.Now
                };
                Customer_BLL.Inserttb_Customer(obj);
                ((Customer)Page).GetCustomer();
                ClearTextBox();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công nhập vật tư !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaKhachHang.Enabled = true;
                drQuanHuyen.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateCustomer()
        {
            try
            {
                var obj = new Customer_ET
                {
                    Customer_ID = int.Parse(lbID.Value),
                    Customer_Alias = txtMaKhachHang.Text,
                    Customer_Type_ID = int.Parse(drLoaiKhachHang.SelectedValue),
                    Customer_Shortcut = txtTenVietTat.Text,
                    Customer_Name = txtTenKhachHang.Text,
                    Customer_Name_EN = txtTenTiengAnh.Text,
                    Customer_Name_Exchange = txtTenGiaoDich.Text,
                    Enterprise_ID = int.Parse(drLoaiHinhDoanhNghiep.SelectedValue),
                    Customer_Debt_Limit = float.Parse(txtHanMucNo.Text),
                    Customer_Address = txtDiaChiGiaoDich.Text,
                    Customer_Payment_Deadline = int.Parse(txtHanThanhToan.Text),
                    District_ID = int.Parse(drQuanHuyen.SelectedValue),
                    Customer_Establish = DateTime.Parse(txtNgayThanhLap.Text),
                    Customer_Phone1 = txtDienThoai1.Text,
                    Customer_Phone2 = txtDienThoai2.Text,
                    Customer_Fax = txtFax.Text,
                    Customer_Website = txtWebsite.Text,
                    Sector_ID = int.Parse(drNganhNghe.SelectedValue),
                    Customer_Account_Number = txtSoTaiKhoan.Text,
                    Customer_Contact = int.Parse(drNguoiLienHe.SelectedValue),
                    Bank_ID = int.Parse(drNganHang.SelectedValue),
                    Customer_Contract_FullName = txtTenDayDu.Text,
                    Customer_Contract_Address = txtDiaChiHopDong.Text,
                    Customer_Contract_District = int.Parse(drQuanHuyenHopDong.SelectedValue),
                    Customer_Contract_Tax_Code = txtMaSoThue.Text,
                    Customer_Contract_Contact = int.Parse(drNguoiKyHopDong.SelectedValue),
                    Customer_Delivery_Address = txtDiaChiGiaoNhan.Text,
                    Customer_Delivery_Contact = int.Parse(drNguoiNhan.SelectedValue),
                    Customer_Payment_Address = txtDiaChiThanhToan.Text,
                    Customer_Payment_Contact = int.Parse(drNguoiThanhToan.SelectedValue),
                    Customer_Note = txtGhiChu.Text,
                    Customer_Date_Add = DateTime.Now,
                    Customer_Date_Modify = DateTime.Now
                };
                Customer_BLL.Updatetb_Customer(obj);
                ((Customer)Page).GetCustomer();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công nhập vật tư !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaKhachHang.Enabled = false;
                drQuanHuyen.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void BindGridDrop()
        {
            var clsLoaiKhachHang = new Customer_Type_BLL();
            var dtLoaiKhachHang = clsLoaiKhachHang.BindGirdtb_Customer_Type(string.Empty);
            if (dtLoaiKhachHang != null && dtLoaiKhachHang.Rows.Count > 0)
            {
                drLoaiKhachHang.Items.Clear();
                drLoaiKhachHang.Items.Add(new ListItem("-Vui lòng chọn loại khách hàng-", "-1"));
                drLoaiKhachHang.DataSource = dtLoaiKhachHang;
                drLoaiKhachHang.DataValueField = "Customer_Type_Id";
                drLoaiKhachHang.DataTextField = "Customer_Type_Name";
                drLoaiKhachHang.DataBind();
            }

            var clsLoaiDN = new Enterprise_BLL();
            var dtLoaiDN = clsLoaiDN.BindGirdEnterprise(string.Empty);
            if (dtLoaiDN != null && dtLoaiDN.Rows.Count > 0)
            {
                drLoaiHinhDoanhNghiep.Items.Clear();
                drLoaiHinhDoanhNghiep.Items.Add(new ListItem("-Vui lòng chọn doanh nghiệp-", "-1"));
                drLoaiHinhDoanhNghiep.DataSource = dtLoaiDN;
                drLoaiHinhDoanhNghiep.DataValueField = "Enterprise_Id";
                drLoaiHinhDoanhNghiep.DataTextField = "Enterprise_Name";
                drLoaiHinhDoanhNghiep.DataBind();
            }

            var clsTinh = new Province_BLL();
            var dtTinh = clsTinh.BindGirdProvince(string.Empty);
            if (dtTinh != null && dtTinh.Rows.Count > 0)
            {
                drTinh.Items.Clear();
                drTinh.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drTinh.DataSource = dtTinh;
                drTinh.DataValueField = "Province_Id";
                drTinh.DataTextField = "Province_Name";
                drTinh.DataBind();

                drTinhHopDong.Items.Clear();
                drTinhHopDong.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drTinhHopDong.DataSource = dtTinh;
                drTinhHopDong.DataValueField = "Province_Id";
                drTinhHopDong.DataTextField = "Province_Name";
                drTinhHopDong.DataBind();
            }

            var clsQuanHuyen = new Districts_BLL();
            var dtQuanHuyen = clsQuanHuyen.BindGirdDistricts(string.Empty);
            if (dtQuanHuyen != null && dtQuanHuyen.Rows.Count > 0)
            {
                drQuanHuyen.Items.Clear();
                drQuanHuyen.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drQuanHuyen.DataSource = dtQuanHuyen;
                drQuanHuyen.DataValueField = "Districts_Id";
                drQuanHuyen.DataTextField = "Districts_Name";
                drQuanHuyen.DataBind();

                drQuanHuyenHopDong.Items.Clear();
                drQuanHuyenHopDong.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drQuanHuyenHopDong.DataSource = dtQuanHuyen;
                drQuanHuyenHopDong.DataValueField = "Districts_Id";
                drQuanHuyenHopDong.DataTextField = "Districts_Name";
                drQuanHuyenHopDong.DataBind();
            }

            var clsNganhNghe = new Sector_BLL();
            var dtNganhNghe = clsNganhNghe.BindGirdSector(string.Empty);
            if (dtNganhNghe != null && dtNganhNghe.Rows.Count > 0)
            {
                drNganhNghe.Items.Clear();
                drNganhNghe.Items.Add(new ListItem("-Vui lòng chọn ngành nghề-", "-1"));
                drNganhNghe.DataSource = dtNganhNghe;
                drNganhNghe.DataValueField = "Sector_Id";
                drNganhNghe.DataTextField = "Sector_Name";
                drNganhNghe.DataBind();
            }

            var clsNguoiLienHe = new Contact_BLL();
            var dtNguoiLienHe = clsNguoiLienHe.BindGirdContact(string.Empty);
            if (dtNguoiLienHe != null && dtNguoiLienHe.Rows.Count > 0)
            {
                drNguoiLienHe.Items.Clear();
                drNguoiLienHe.Items.Add(new ListItem("-Vui lòng chọn người liên hệ-", "-1"));
                drNguoiLienHe.DataSource = dtNguoiLienHe;
                drNguoiLienHe.DataValueField = "Contact_Id";
                drNguoiLienHe.DataTextField = "Contact_Name";
                drNguoiLienHe.DataBind();

                drNguoiKyHopDong.Items.Clear();
                drNguoiKyHopDong.Items.Add(new ListItem("-Vui lòng chọn người liên hệ-", "-1"));
                drNguoiKyHopDong.DataSource = dtNguoiLienHe;
                drNguoiKyHopDong.DataValueField = "Contact_Id";
                drNguoiKyHopDong.DataTextField = "Contact_Name";
                drNguoiKyHopDong.DataBind();

                drNguoiNhan.Items.Clear();
                drNguoiNhan.Items.Add(new ListItem("-Vui lòng chọn người liên hệ-", "-1"));
                drNguoiNhan.DataSource = dtNguoiLienHe;
                drNguoiNhan.DataValueField = "Contact_Id";
                drNguoiNhan.DataTextField = "Contact_Name";
                drNguoiNhan.DataBind();

                drNguoiThanhToan.Items.Clear();
                drNguoiThanhToan.Items.Add(new ListItem("-Vui lòng chọn người liên hệ-", "-1"));
                drNguoiThanhToan.DataSource = dtNguoiLienHe;
                drNguoiThanhToan.DataValueField = "Contact_Id";
                drNguoiThanhToan.DataTextField = "Contact_Name";
                drNguoiThanhToan.DataBind();
            }

            var clsNganHang = new Bank_BLL();
            var dtNganHang = clsNganHang.BindGirdtb_Bank(string.Empty);
            if (dtNganHang != null && dtNganHang.Rows.Count > 0)
            {
                drNganHang.Items.Clear();
                drNganHang.Items.Add(new ListItem("-Vui lòng chọn ngân hàng-", "-1"));
                drNganHang.DataSource = dtNganHang;
                drNganHang.DataValueField = "Bank_Id";
                drNganHang.DataTextField = "Bank_Name";
                drNganHang.DataBind();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaKhachHang.Text = string.Empty;
            txtTenVietTat.Text = string.Empty;
            txtTenKhachHang.Text = string.Empty;
            txtTenTiengAnh.Text = string.Empty;
            txtHanMucNo.Text = "0";
            txtTenGiaoDich.Text = string.Empty;
            txtDiaChiGiaoDich.Text = string.Empty;
            txtHanThanhToan.Text = "0";
            txtNgayThanhLap.Text = DateTime.Now.ToString();
            txtDienThoai1.Text = string.Empty;
            txtDienThoai2.Text = string.Empty;
            txtFax.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtWebsite.Text = string.Empty;
            txtSoTaiKhoan.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtDienThoai.Text = string.Empty;
            txtDiDong.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtTenDayDu.Text = string.Empty;
            txtDiaChiHopDong.Text = string.Empty;
            txtMaSoThue.Text = string.Empty;
            txtChucVuHopDong.Text = string.Empty;
            txtDienThoaiHopDong.Text = string.Empty;
            txtDiDongHopDong.Text = string.Empty;
            txtEmailHopDong.Text = string.Empty;
            txtDiaChiGiaoNhan.Text = string.Empty;
            txtChucVuNguoiNhan.Text = string.Empty;
            txtDienThoaiNguoinhan.Text = string.Empty;
            txtDiDongNguoiNhan.Text = string.Empty;
            txtEmailNguoiNhan.Text = string.Empty;
            txtDiaChiThanhToan.Text = string.Empty;
            txtChucVuThanhToan.Text = string.Empty;
            txtDienThoaiThanhToan.Text = string.Empty;
            txtDiDongThanhToan.Text = string.Empty;
            txtEmailThanhToan.Text = string.Empty;
            txtGhiChu.Text = string.Empty;

            drLoaiKhachHang.SelectedIndex = 0;
            drLoaiHinhDoanhNghiep.SelectedIndex = 0;
            drTinh.SelectedIndex = 0;
            drQuanHuyen.SelectedIndex = 0;
            drNganhNghe.SelectedIndex = 0;
            drNguoiLienHe.SelectedIndex = 0;
            drNganHang.SelectedIndex = 0;
            drTinhHopDong.SelectedIndex = 0;
            drQuanHuyenHopDong.SelectedIndex = 0;
            drNguoiKyHopDong.SelectedIndex = 0;
            drNguoiNhan.SelectedIndex = 0;
            drNguoiThanhToan.SelectedIndex = 0;
        }
        #endregion

        protected void drTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drTinh.SelectedIndex == 0)
            {
                drQuanHuyen.Enabled = false;
            }
            else
            {
                drQuanHuyen.Enabled = true;
                var clsQuanHuyen = new Districts_BLL();
                var dtQuanHuyen = clsQuanHuyen.BindGirdDistricts_Province(int.Parse(drTinh.SelectedValue));
                if (dtQuanHuyen != null && dtQuanHuyen.Rows.Count > 0)
                {
                    drQuanHuyen.Items.Clear();
                    drQuanHuyen.Items.Add(new ListItem("-Vui lòng chọn quận huyện-", "-1"));
                    drQuanHuyen.DataSource = dtQuanHuyen;
                    drQuanHuyen.DataValueField = "Districts_Id";
                    drQuanHuyen.DataTextField = "Districts_Name";
                    drQuanHuyen.DataBind();
                }
                else
                {
                    drQuanHuyen.Enabled = false;
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Tỉnh này không tồn tại quận huyện vui lòng chọn tỉnh khác !";
                    OpenDialog("#thongbao");
                    if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    drTinh.SelectedIndex = 0;
                }
            }
        }

        protected void drTinhHopDong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drTinhHopDong.SelectedIndex == 0)
            {
                drQuanHuyenHopDong.Enabled = false;
            }
            else
            {
                drQuanHuyenHopDong.Enabled = true;
                var clsQuanHuyenHopDong = new Districts_BLL();
                var dtQuanHuyenHopDong = clsQuanHuyenHopDong.BindGirdDistricts_Province(int.Parse(drTinhHopDong.SelectedValue));
                if (dtQuanHuyenHopDong != null && dtQuanHuyenHopDong.Rows.Count > 0)
                {
                    drQuanHuyenHopDong.Items.Clear();
                    drQuanHuyenHopDong.Items.Add(new ListItem("-Vui lòng chọn quận huyện-", "-1"));
                    drQuanHuyenHopDong.DataSource = dtQuanHuyenHopDong;
                    drQuanHuyenHopDong.DataValueField = "Districts_Id";
                    drQuanHuyenHopDong.DataTextField = "Districts_Name";
                    drQuanHuyenHopDong.DataBind();
                }
                else
                {
                    drQuanHuyenHopDong.Enabled = false;
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Tỉnh này không tồn tại quận huyện vui lòng chọn tỉnh khác !";
                    OpenDialog("#thongbao");
                    if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    drTinhHopDong.SelectedIndex = 0;
                }
            }
        }

        protected void drNguoiLienHe_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtChucVu.Text = string.Empty;
            txtDienThoai.Text = string.Empty;
            txtDiDong.Text = string.Empty;
            txtEmail.Text = string.Empty;
            var clsNguoiLienHe = new Contact_BLL();
            var dtNguoiLienHe = clsNguoiLienHe.BindGirdContactDetail(int.Parse(drNguoiLienHe.SelectedValue));
            if (dtNguoiLienHe != null)
            {
                txtChucVu.Text = dtNguoiLienHe.Rows[0]["Job_Name"].ToString();
                txtDienThoai.Text = dtNguoiLienHe.Rows[0]["Contact_Phone"].ToString();
                txtDiDong.Text = dtNguoiLienHe.Rows[0]["Contact_MobilePhone"].ToString();
                txtEmail.Text = dtNguoiLienHe.Rows[0]["Contact_Email"].ToString();
            }
        }

        protected void drNguoiKyHopDong_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtChucVuHopDong.Text = string.Empty;
            txtDienThoaiHopDong.Text = string.Empty;
            txtDiDongHopDong.Text = string.Empty;
            txtEmailHopDong.Text = string.Empty;
            var clsNguoiLienHe = new Contact_BLL();
            var dtNguoiLienHe = clsNguoiLienHe.BindGirdContactDetail(int.Parse(drNguoiLienHe.SelectedValue));
            if (dtNguoiLienHe != null)
            {
                txtChucVuHopDong.Text = dtNguoiLienHe.Rows[0]["Job_Name"].ToString();
                txtDienThoaiHopDong.Text = dtNguoiLienHe.Rows[0]["Contact_Phone"].ToString();
                txtDiDongHopDong.Text = dtNguoiLienHe.Rows[0]["Contact_MobilePhone"].ToString();
                txtEmailHopDong.Text = dtNguoiLienHe.Rows[0]["Contact_Email"].ToString();
            }
        }

        protected void drNguoiNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtChucVuNguoiNhan.Text = string.Empty;
            txtDienThoaiNguoinhan.Text = string.Empty;
            txtDiDongNguoiNhan.Text = string.Empty;
            txtEmailNguoiNhan.Text = string.Empty;
            var clsNguoiLienHe = new Contact_BLL();
            var dtNguoiLienHe = clsNguoiLienHe.BindGirdContactDetail(int.Parse(drNguoiLienHe.SelectedValue));
            if (dtNguoiLienHe != null)
            {
                txtChucVuNguoiNhan.Text = dtNguoiLienHe.Rows[0]["Job_Name"].ToString();
                txtDienThoaiNguoinhan.Text = dtNguoiLienHe.Rows[0]["Contact_Phone"].ToString();
                txtDiDongNguoiNhan.Text = dtNguoiLienHe.Rows[0]["Contact_MobilePhone"].ToString();
                txtEmailNguoiNhan.Text = dtNguoiLienHe.Rows[0]["Contact_Email"].ToString();
            }
        }

        protected void drNguoiThanhToan_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtChucVuThanhToan.Text = string.Empty;
            txtDienThoaiThanhToan.Text = string.Empty;
            txtDiDongThanhToan.Text = string.Empty;
            txtEmailThanhToan.Text = string.Empty;
            var clsNguoiLienHe = new Contact_BLL();
            var dtNguoiLienHe = clsNguoiLienHe.BindGirdContactDetail(int.Parse(drNguoiLienHe.SelectedValue));
            if (dtNguoiLienHe != null)
            {
                txtChucVuThanhToan.Text = dtNguoiLienHe.Rows[0]["Job_Name"].ToString();
                txtDienThoaiThanhToan.Text = dtNguoiLienHe.Rows[0]["Contact_Phone"].ToString();
                txtDiDongThanhToan.Text = dtNguoiLienHe.Rows[0]["Contact_MobilePhone"].ToString();
                txtEmailThanhToan.Text = dtNguoiLienHe.Rows[0]["Contact_Email"].ToString();
            }
        }
    }
}